//
//  HomeViewController.m
//  CNKICommunity
//
//  Created by wei on 17/1/23.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "HomeViewController.h"
#import "adddViewController.h"
#import "MMDrawerController.h"
#import "SencondViewController.h"
#import "MMDrawerBarButtonItem.h"
#import "CNKISearchViewController.h"
#import "CNKISearchResultViewController.h"
#import "CNKINavViewController.h"
#import "CNKIChanelViewController.h"
@interface HomeViewController ()<CNKISearchVCdelegate>
@property(nonatomic,strong)MMDrawerController *MMC;

@end

@implementation HomeViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(leftbtn)];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
    imageView.image = [UIImage imageNamed:@"tabBar_publish_icon"];
    //[self.view addSubview:imageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpNextPage)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [imageView addGestureRecognizer:tap];
    imageView.userInteractionEnabled = YES;
    
    //[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeCustom];
    
    [self setUpButton];

    //2、添加双击手势
    UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    //2.1、双击
    [doubleTap setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleTap];
    
    //3、添加两个手指双击手势
    UITapGestureRecognizer * twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerDoubleTap:)];
    //3.1、双击
    [twoFingerDoubleTap setNumberOfTapsRequired:2];
    //3.2、两个手指  默认为一个
    [twoFingerDoubleTap setNumberOfTouchesRequired:2];
    [self.view addGestureRecognizer:twoFingerDoubleTap];

    
    
}

-(void)leftbtn
{
    _MMC = (MMDrawerController*)[UIApplication sharedApplication].keyWindow.rootViewController;
    [_MMC toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}


-(void)setUpButton
{
    for (int i = 0; i < 5; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor lightGrayColor];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
         button.font =[UIFont systemFontOfSize:17];
        button.frame = CGRectMake(40, i * 60 + 20 * i + 70, SCREENWIDTH - 80, 50);
        [self.view addSubview:button];
        switch (i) {
            case 0:
                [button setTitle:@"学科选择页面" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(jumpNextPage) forControlEvents:UIControlEventTouchUpInside];
                break;
            case 1:
                [button setTitle:@"问题点赞页面" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(jumpLovePage) forControlEvents:UIControlEventTouchUpInside];
                break;
            case 2:
                [button setTitle:@"搜索页面" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(jumpFindPage) forControlEvents:UIControlEventTouchUpInside];
                break;
            case 3:
                [button setTitle:@"频道选择" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(jumpChanelPage) forControlEvents:UIControlEventTouchUpInside];
                break;
            default:
                break;
        }
        
    }
}
/**
 *  添加点击手势  一个手指双击
 */
-(void)doubleTap:(UITapGestureRecognizer*)gesture{
    [_MMC bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}

/**
 *  添加点击手势  两个个手指双击
 */
-(void)twoFingerDoubleTap:(UITapGestureRecognizer*)gesture{
    [_MMC bouncePreviewForDrawerSide:MMDrawerSideRight completion:nil];
}

-(void)jumpNextPage
{
    [self.navigationController pushViewController:[adddViewController new] animated:YES];
}
-(void)jumpLovePage
{
    [self.navigationController pushViewController:[SencondViewController new] animated:YES];
}
-(void)jumpChanelPage
{
    [self.navigationController pushViewController:[CNKIChanelViewController new] animated:YES];
}
-(void)jumpFindPage
{
    NSArray *arr = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"];
    CNKISearchViewController *MysearchViewcontroller = [CNKISearchViewController setupSearchVCWithHotSearches:arr searchPlaceholder:Nil Searchblock:^(CNKISearchViewController *searchVC, UISearchBar *searchBar, NSString *searchText) {
        //拿到检索词 传到结果页面去请求
        dispatch_async(dispatch_get_main_queue(), ^{
           [searchVC.navigationController pushViewController:[[CNKISearchResultViewController alloc]init] animated:YES];
        });
        
    }];
    MysearchViewcontroller.SearchDelegate = self;
    //包装Nav
    CNKINavViewController *nav = [[CNKINavViewController alloc]initWithRootViewController:MysearchViewcontroller];
    [self presentViewController:nav animated:NO completion:nil];
    
}
-(void)searchVC:(CNKISearchViewController *)vc searchTextDidChange:(UISearchBar *)bar searchText:(NSString *)searchText
{
    if (searchText.length)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //显示建议结果
            NSMutableArray *searchArr = [NSMutableArray array];
            for (int i = 0; i < arc4random_uniform(5) + 10; i++)
            {
                NSString *str = [NSString stringWithFormat:@"关于%@:第%d条",searchText,i];
                [searchArr addObject:str];
            }
            vc.searchSuggestions = searchArr;

        });
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
